package com.wywah.yunduo.security.supports.form;

import cn.hutool.core.util.CharsetUtil;
import com.wywah.common.utils.ServletUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;

public class FormAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
//        String url = HttpUtil.encodeParams(String.format("/token/login?error=%s", exception.getMessage()),
//                CharsetUtil.CHARSET_UTF_8);
        String url = String.format("/token/login?error=%s", exception.getMessage());
        ServletUtils.getResponse().sendRedirect(url);
    }
}
